package com.ygxsk.carhome.ui.leads;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.flyco.roundview.RoundTextView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.enumwrap.LeadCarStepStatus;
import com.ygxsk.carhome.listener.ButtonLeadFollowUpListener;
import com.ygxsk.carhome.listener.RxBus;
import com.ygxsk.carhome.response.LeadsFollowUpInfoBean;
import com.ygxsk.carhome.rxbus.RxBusFollowStep;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.views.FJEditTextCountPublish;

import java.util.ArrayList;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.functions.Consumer;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-07-13  10:15
 * @Description:添加记录的弹窗内容
 */
public class RecordEditPopu extends BottomPopupView {

    private Context currentContext;
    private Activity currentActivity;

    //因为通用界面，标志从哪个页面过来；
    private int extra_jumpfrom_where;
    //携带过来的状态，
    private int extra_currentSate;

    //该界面的外侧的弹窗
    private BasePopupView basePopupView_followUp;

    private ButtonLeadFollowUpListener buttonLeadFollowUpListener;

    private ArrayList<LeadsFollowUpInfoBean> mDataResources_followUp = new ArrayList<>();

    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();

    //页面通用的弹窗
//    private AlertView mAlertView;

    private BasePopupView mAlertBasePopupView;

    //跟进状态 的传递
    private String status_upLoad = "";

    public RecordEditPopu(@NonNull Context context,//
                          Activity _activity,//
                          int _extra_action,//
                          int currentSate,//
                          ArrayList<LeadsFollowUpInfoBean> _stationDatas,//
                          ButtonLeadFollowUpListener _buttonLeadFollowUpListener) {//
        super(context);

        //不这样等于，直接 赋值 添加；
        //station_list_outSide = _stationDatas;
        extra_jumpfrom_where = _extra_action;
        extra_currentSate = currentSate;
        currentContext = context;
        currentActivity = _activity;
        buttonLeadFollowUpListener = _buttonLeadFollowUpListener;

        if (_stationDatas != null && !_stationDatas.isEmpty()) {

            if (mDataResources_followUp != null && !mDataResources_followUp.isEmpty()) {
                mDataResources_followUp.clear();
            }

            if (mDataResources_followUp != null) {
                mDataResources_followUp.addAll(_stationDatas);
            }

        }

    }

    @Override
    protected int getImplLayoutId() {//当前的这个条目内容
        //return R.layout.assetaddsubmitadapter;
        return R.layout.recordeditpopu;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        //查找布局的id
//        RelativeLayout layoutFollowUp = (RelativeLayout)this.findViewById(R.id.layoutfollowuppopu);
        LeadsFollowUpInfoBean leadsFollowUpInfoBean1 = new LeadsFollowUpInfoBean();
        leadsFollowUpInfoBean1.setDictCode(LeadCarStepStatus.LEAD_STEP_TO_BE_CONTACT.getIndex());
        leadsFollowUpInfoBean1.setDictLabel(LeadCarStepStatus.LEAD_STEP_TO_BE_CONTACT.getValue());

        LeadsFollowUpInfoBean leadsFollowUpInfoBean2 = new LeadsFollowUpInfoBean();
        leadsFollowUpInfoBean2.setDictCode(LeadCarStepStatus.LEAD_STEP_TO_INIT_CONTACT.getIndex());
        leadsFollowUpInfoBean2.setDictLabel(LeadCarStepStatus.LEAD_STEP_TO_INIT_CONTACT.getValue());

        LeadsFollowUpInfoBean leadsFollowUpInfoBean3 = new LeadsFollowUpInfoBean();
        leadsFollowUpInfoBean3.setDictCode(LeadCarStepStatus.LEAD_STEP_HAS_A_INTENT.getIndex());
        leadsFollowUpInfoBean3.setDictLabel(LeadCarStepStatus.LEAD_STEP_HAS_A_INTENT.getValue());

        LeadsFollowUpInfoBean leadsFollowUpInfoBean4 = new LeadsFollowUpInfoBean();
        leadsFollowUpInfoBean4.setDictCode(LeadCarStepStatus.LEAD_STEP_HAS_NO_INTENT.getIndex());
        leadsFollowUpInfoBean4.setDictLabel(LeadCarStepStatus.LEAD_STEP_HAS_NO_INTENT.getValue());

        LeadsFollowUpInfoBean leadsFollowUpInfoBean5 = new LeadsFollowUpInfoBean();
        leadsFollowUpInfoBean5.setDictCode(LeadCarStepStatus.LEAD_STEP_SUCCESS.getIndex());
        leadsFollowUpInfoBean5.setDictLabel(LeadCarStepStatus.LEAD_STEP_SUCCESS.getValue());

        LeadsFollowUpInfoBean leadsFollowUpInfoBean6 = new LeadsFollowUpInfoBean();
        leadsFollowUpInfoBean6.setDictCode(LeadCarStepStatus.LEAD_STEP_FAIL.getIndex());
        leadsFollowUpInfoBean6.setDictLabel(LeadCarStepStatus.LEAD_STEP_FAIL.getValue());

        LeadsFollowUpInfoBean leadsFollowUpInfoBean7 = new LeadsFollowUpInfoBean();
        leadsFollowUpInfoBean7.setDictCode(LeadCarStepStatus.LEAD_STEP_CANCEL_BOOKING.getIndex());
        leadsFollowUpInfoBean7.setDictLabel(LeadCarStepStatus.LEAD_STEP_CANCEL_BOOKING.getValue());

        mDataResources_followUp.add(leadsFollowUpInfoBean1);
        mDataResources_followUp.add(leadsFollowUpInfoBean2);
        mDataResources_followUp.add(leadsFollowUpInfoBean3);
        mDataResources_followUp.add(leadsFollowUpInfoBean4);
        mDataResources_followUp.add(leadsFollowUpInfoBean5);
        mDataResources_followUp.add(leadsFollowUpInfoBean6);
        mDataResources_followUp.add(leadsFollowUpInfoBean7);

        //对状态值做判断
        //bug-2024年1月26日15:50:14 处于该状态的，不能操作它状态之前的操作。
        //大于该状态值的都可用，而低于该状态值的都不可用。
        //当前传递过来的详情状态：
        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "extra_currentSate-->" + extra_currentSate);

//        for (LeadsFollowUpInfoBean item : mDataResources_followUp) {
//
//            String itemState = item.getDictCode();
//
//            if (!TextUtils.isEmpty(itemState)) {
//
//                int itemStateInt = Integer.parseInt(itemState);
//
//                //如果当前所处于的状态大于遍历的状态，如果等于该状态，也是不能再选的。
//                if (extra_currentSate >= itemStateInt) {
//                    item.setCanCheck(false);
//                } else {
//                    item.setCanCheck(true);
//                }
//
//            }
//
//        }


        //---------------------------------------------------------------------------------------------------------------------------------------------------

        //外侧页面布局
        LinearLayout layoutFollowUpPopu = (LinearLayout) this.findViewById(R.id.layoutfollowuppopu);
        //填充文本信息
        TextView textViewEditFollowUp = (TextView) this.findViewById(R.id.textvieweditfollowuppopu);
        //填充 可编辑的备注信息
        FJEditTextCountPublish editViewContentPulishInRentandSell = (FJEditTextCountPublish) this.findViewById(R.id.editviewcontentpulishinrentandsellpopu);

        //两个按钮的内容  取消 和 确定
        RoundTextView textViewModifyChange = (RoundTextView) this.findViewById(R.id.textviewmodifychange);
        RoundTextView textViewComplete = (RoundTextView) this.findViewById(R.id.textviewcomplete);

        if (layoutFollowUpPopu != null) {
            layoutFollowUpPopu.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {

                    //弹窗是跟进状态的
                    if (mDataResources_followUp != null && !mDataResources_followUp.isEmpty()) {

                        KeyboardUtils.hideSoftInput(currentActivity);

                        LeadsFollowUpPopu leadsFollowUpPopu = new LeadsFollowUpPopu(
                                currentActivity,//
                                currentContext,//
                                ExtraValue.VALUE_JUMP_FORM_LEADADDSUBMITACTIVITY_FOLLOWUP_TOPPOPU,//添加跟进进度的标识；
                                mDataResources_followUp);

                        //请选择跟进状态
                        leadsFollowUpPopu.setTitleName(getResources().getString(R.string.choose_title_step_state));

                        basePopupView_followUp = new XPopup.Builder(currentContext)
                                .hasShadowBg(true)
                                //.moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面;
                                .isViewMode(true)
                                //.autoDismiss(false)
                                //.isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                                //.isThreeDrag(false) //是否开启三阶拖拽，如果设置enableDrag(false)则无效
                                //.dismissOnTouchOutside(true)
                                //.notDismissWhenTouchInView()
                                //.asCustom(new SelectViewCustomPopup(ExtraValue.RXBUS_WORKBOOK_STARTDATE_TO_ADDSUBMIT, ManagerCheckAddSubmitActivity.this))
                                //.moveUpToKeyboard(false)
                                .asCustom(leadsFollowUpPopu)//
                                .show();

                    } else {

                        //重新获取一遍接口；
                        // TODO: 2023-07-13 11:18 重新获取一遍接口信息，之后再填充


                    }

                }
            });

        }

        if (textViewComplete != null) {
            textViewComplete.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {

                    // : 2023-07-13 14:29 需要携带什么信息？跟进状态，和备注信息？
                    //校验【跟进状态的】的文本输入
                    if (TextUtils.isEmpty(status_upLoad)
                            || TextUtils.isEmpty(textViewEditFollowUp.getText().toString().trim())) {

                        //因为弹窗被底部的编辑框遮盖，这里要找其他的弹窗代替
//                        CommUtils.checkDialog(mAlertView);
//                        mAlertView = CommUtils.method_showAlertViewSingleSlowly(//
//                                currentContext,//
//                                ConstantSign.ALERT_TITLE,//
//                                currentContext.getString(R.string.check_customer_followstep_upload),//校验跟进状态
//                                true);//
//                        mAlertView.show();

                        CommUtils.checkBasePopupView(mAlertBasePopupView);
                        mAlertBasePopupView = CommUtils.method_showBasePopupViewSingle(currentActivity, currentContext, currentContext.getString(R.string.check_customer_followstep_upload));

                        return;
                    }

                    //应该是调用一个提交接口的
                    if (buttonLeadFollowUpListener != null) {
                        //携带 选中的状态，和 输入的备注信息
                        buttonLeadFollowUpListener.buttonLeadFollowUp(status_upLoad, editViewContentPulishInRentandSell.getText().toString().trim());
                    }

                }
            });
        }


        if (textViewModifyChange != null) {
            textViewModifyChange.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {

                    //清空信息
                    if (textViewEditFollowUp != null) {
                        textViewEditFollowUp.setText("");
                    }

                    //备注信息清空
                    if (editViewContentPulishInRentandSell != null) {
                        editViewContentPulishInRentandSell.setText("");
                    }

                    //关闭 一个弹窗
                    RecordEditPopu.this.dismiss();
                }
            });
        }

        //对选中逻辑的监听
        mCompositeDisposable.add(RxBus.getInstance()//
                .toObserverable(RxBusFollowStep.class)//
                .subscribeOn(AndroidSchedulers.mainThread())//
                .observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new Consumer<RxBusFollowStep>() {//
                    @Override
                    public void accept(RxBusFollowStep rxBusFollowStep) throws Exception {

                        int actionFlag = rxBusFollowStep.getActionFlag();
                        LeadsFollowUpInfoBean leadsFollowUpInfoBean = rxBusFollowStep.getLeadsFollowUpInfoBean();

                        if (actionFlag == ExtraValue.MODIFY_LEAD_FOLLOW_STEP_SELECT) {//更改变动跟进状态的信息

                            status_upLoad = leadsFollowUpInfoBean.getDictCode();

//                            LogUtils.d("itchen---mCompositeDisposable---切换 跟进状态-->" + leadsFollowUpInfoBean);

                            if (textViewEditFollowUp != null) {
                                textViewEditFollowUp.setText(leadsFollowUpInfoBean.getDictLabel());
                            }

                        }
                    }
                }));

    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .6f);
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        CommUtils.checkBasePopupView(mAlertBasePopupView);
    }
}
